package com.lobobrowser.encoder;

import java.io.IOException;

import sun.misc.BASE64Decoder;

public class Encoder {

	private String key;
	
	public Encoder(String key){
		this.key = key;
	}
	
	public String encode(String str){
		
		String encodedStr = "";

		for(int i = 0; i< str.length(); i++){
//			encodedStr = encodedStr + (str.charAt(i) ^ keystr.charAt(i % keystr.length()));
			char en = (char)(str.charAt(i) ^ this.key.charAt(i % this.key.length()));
			encodedStr = encodedStr + en;
//			char dn = (char)(en ^ keystr.charAt(i % keystr.length()));
//			System.out.println("en: "+ en + " - dn: " +dn );
		
		}
		return encodedStr;
	}
	
	public String decode(String str){
		BASE64Decoder b64str = new BASE64Decoder();
		
		String keystr = this.key;
		byte[] encodedStr = null;
		try {
			encodedStr = b64str.decodeBuffer(str);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String dStr = new String(encodedStr);
		System.out.println(dStr + " - " + dStr.length());
		
		String res = "";
		for(int i = 0; i< dStr.length(); i++){
			char en = (char)(dStr.charAt(i) ^ keystr.charAt(i % keystr.length()));
			res = res + en;
			
		  
		}

		
		System.out.println("returning from decoder");
		return res;
	}
	
	public static void main(String [] args) {
//		Key k = new Key ("ae");
		BASE64Decoder a;
		Encoder en = new Encoder ("^u");
		String result = en.encode("7,3");
		System.out.println("Encoded str: " + result);
		result = en.encode(result);
		System.out.println("Decoded str: " + result);
		
		
	}
}
